const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: { 
        name:"",
        page: {
            searchCount: false,
            current: 1,
            size: 10,
          },
      dataList:[],
      cateList: [],
      curIndex: 0,
      userInfo:''
    },
    onShow() {
      app.initPage().then(res => {
        this.setData({
          userInfo: res
        })
        this.getCate()
        this.getVideoList(true)
      })
    },
    onClick(){
        this.setData({dataList:[]})
        this.getVideoList()
    },
    onChange(e){
        console.log(e)
        this.setData({name:e.detail})
       
    },
    toDetail(e){
      if(this.data.userInfo.auths == 0){
        wx.showModal({
          title: '提示',
          showCancel: false,
          content: '此功能仅对星程计划学员开放，详细请咨询【启航星程SV小助手】（微信号：starvoyage01）',
          complete: (res) => { 
          }
        })
        return
      }
      var id = e.currentTarget.dataset.id
      wx.navigateTo({
        url: `/pages/save/vodeoDetail/index?id=${id}`,
      })
    },
    getVideoList(isClear){
      if(isClear){
        this.setData({
          dataList: []
        })
      }
        app.api.getVideoList({
            searchCount: false,
            current: this.data.page.current,
            size: this.data.page.size,
            cateId: this.data.curIndex == 0 ? ''  
                    : this.data.cateList[this.data.curIndex].id,
            title: this.data.name
            // descs: 'create_time'
          })
            .then(res => { 
              res.data.records.forEach(item => {
                item.createTime = item.createTime.substring(0,10)
              })
              var videoLsit = res.data.records; 
              this.setData({
                dataList: [...this.data.dataList, ...videoLsit]
              })
            })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
     
    },
    getCate(){
      app.api.getWishCate({id:3}).then(res => {
        res.data.unshift({id:0,name:'全部'})
        this.setData({
          cateList: res.data
        })
        this.getList()
      })
    },
    getList(){

    },
    // 切换tab
    cateSwitch(e){
      this.setData({
        ['page.current']: 1,
        curIndex: e.currentTarget.dataset.index,
        wishList:[]
      })
      this.getVideoList(true)
    },

    

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
 
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
      this.setData({
        ['page.current']: this.data.page.current + 1
      })
      this.getVideoList()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})